package org.qydjk.console.controller.basic;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.product.ProductAttr;
import org.qydjk.common.pojo.product.ProductAttrDetail;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.util.ListUtil;
import org.qydjk.common.util.StringUtil;
import org.qydjk.console.service.IAttributeService;
import org.qydjk.console.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *属性管理Controllor
 * Created by Edward_yang on 2017/12/13 0013.
 */
@Controller
@RequestMapping("/attribute")
public class AttributeControllor extends BaseController {
    @Autowired
    private IAttributeService attributeService;
    @Autowired
    private ICategoryService categoryService;//分类Service

    /***
     * 页面跳转
     * @author Edward_yang
     * @uptime 2017-12-13
     * @return
     */
    @RequestMapping("/gotoAttribute.do")
    public String gotoAttribute(){
        return "basic/sellerAndproduct/attributeList";
    }


    /***
     * 分类列表查询
     * @author Edward_yang
     * @uptime 2017-12-13
     */
    @RequestMapping("/classifylist.do")
    @ResponseBody
    public Object classifyList(Integer page, Integer rows){
        try {
            return categoryService.findAllCategoryList(page,rows);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /***
     * 分类下拉查询
     * @author Edward_yang
     * @uptime 2017-12-13
     */
    @RequestMapping("/classifyComboboxList.do")
    @ResponseBody
    public Object classifyComboboxList(){
        try {
            return categoryService.findAllList();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    /***
     * 属性列表查询
     * @author Edward_yang
     * @uptime 2017-12-13
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/attributeList.do")
    @ResponseBody
    public Object  attributeList(Integer page, Integer rows ,String one_category_id,String keyWord){
        try {
            return attributeService.findAttrbuteList(page,rows,one_category_id,keyWord);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /***
     * 根据分类ID,构建属性表单
     */
    @RequestMapping("/buildAttributes.do")
    @ResponseBody
    public Object buildAttributes(String classifyIds,Integer productId){

        try {
            //构建从子节点到父节点的所有分类ids
            String catIds =  categoryService.findAllByLeafIds(classifyIds);
            if(StringUtil.isEmptyOrNull(catIds)){
                return renderError("分类错误");
            }
            //查询，产品已配置属性
            List<ProductAttrDetail> selectedAttr = new ArrayList<ProductAttrDetail>();
            if(productId != null && productId.intValue() != 0){
                if(productId.intValue() != -1){
                    selectedAttr = baseService.findByHql("from ProductAttrDetail pad where pad.productId = ?", productId.intValue());
                }

            }else{
                return renderError("参数错误");
            }
            //根据分类获取所有的属性
            List<ProductAttr> pas = baseService.findByHql("from ProductAttr pa where pa.oneCategoryId in (" + catIds + ")");
            if(ListUtil.isNullOrEmpty(pas)){
                return renderSuccess("");
            }
            //构建属性表单
            StringBuffer sb = new StringBuffer("");
            for (ProductAttr pa : pas){
                String values = "";
                for (ProductAttrDetail pad : selectedAttr){
                    if(pad.getProductAttrCode().equals(pa.getCode())){
                        values = pad.getContent();
                    }
                }
                sb.append(pa.getName() + ":<input name='attrs_" + pa.getCode() + "' style='width: 500px;'  type='text' value='" + values + "'><br/><br/>");
            }
            return renderSuccess(sb.toString());
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }


    /***
     * 属性添加
     * @author Edward_yang
     * @uptime 2017-12-13
     */
    @RequestMapping("/add.do")
    @ResponseBody
    public Object addAttribute(ProductAttr productAttr){
        try {
            productAttr.setCreateTime(new Date());//创建时间
            productAttr.setUpdateTime(new Date());
            baseService.save(productAttr);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }
    /***
     * 属性修改
     * @author Edward_yang
     * @uptime 2017-12-13
     */
    @RequestMapping("/edit.do")
    @ResponseBody
    public Object editAttribute(ProductAttr productAttr){
        try {
            ProductAttr productAttr1=baseService.get(ProductAttr.class,productAttr.getId());
            BeanUtil.copyProperties(productAttr,productAttr1);
            productAttr1.setUpdateTime(new Date());//修改时间
            baseService.saveOrUpdate(productAttr1);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }
    /***
     * 属性删除
     * @author Edward_yang
     * @uptime 2017-12-13
     */
    @RequestMapping("/delete.do")
    @ResponseBody
    public Object deleteAttribute(Integer id){
        try {
            ProductAttr ProductAttr=baseService.get(ProductAttr.class,id);
            baseService.delete(ProductAttr);
            return renderSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            return renderError(e.getMessage());
        }
    }


}
